package com.bangcle.library.ui.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.json.JSONObject;

public class TitleBar extends FrameLayout{
    private float density= 1.0f;
    private JSONObject styleConfig=new JSONObject();
    private TextView txtTitle;
    private ImageView ivBack;
    private LinearLayout leftGroup,rightGroup;
    public enum BTN{LEFT,RIGHT}
    public TitleBar(Context context) {
        super(context);
        this.initialize(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initialize(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initialize(context);
    }

    protected void initialize(Context mContext) {
        this.density=mContext.getResources().getDisplayMetrics().density;
        //标题
        this.txtTitle=new TextView(this.getContext());
        txtTitle.setPadding((int)(10*density),(int)(10*density),(int)(10*density),(int)(10*density));
        txtTitle.setGravity(Gravity.CENTER);
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
        super.addView(txtTitle,new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.CENTER));
        //右边工具栏
        this.leftGroup=new LinearLayout(mContext);
        super.addView(leftGroup,new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.LEFT|Gravity.CENTER_VERTICAL));
        //返回按钮
        ivBack=new ImageView(this.getContext());
        ivBack.setPadding((int)(10*density),(int)(10*density),(int)(10*density),(int)(10*density));
        //ivBack.setImageResource(R.drawable.ic_back_1);
        ivBack.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).onBackPressed();
            }
        });
        leftGroup.addView(ivBack,0);
        //右边工具栏
        this.rightGroup=new LinearLayout(mContext);
        super.addView(rightGroup,new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.RIGHT|Gravity.CENTER_VERTICAL));
    }
    public void init(boolean isBack,CharSequence title) {
        txtTitle.setText(title);
        ivBack.setVisibility(isBack?View.VISIBLE:View.GONE);
        removeAllBtns(BTN.LEFT);
        removeAllBtns(BTN.RIGHT);
        setStyle();
    }
    protected void setStyle() {
        //ivBack.setImageResource(R.drawable.ic_back_1);
        //super.setBackgroundResource(R.color.themeColor);
        //txtTitle.setTextColor(styleConfig.optInt("textColor",0xFFFFFFFF));
        //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //    super.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        //}
    }
    public void removeAllBtns(BTN btnType){
        if(btnType==BTN.LEFT){
            int cnt=leftGroup.getChildCount()-1;
            leftGroup.removeViews(1,cnt);
        }else{
            rightGroup.removeAllViews();
        }
    }
    public void addImgBtn(BTN btnType,Object tag,int resId,View.OnClickListener l){
        ImageView imgBtn=new ImageView(this.getContext());
        imgBtn.setTag(tag);
        imgBtn.setImageResource(resId);
        imgBtn.setPadding((int)(10*density),(int)(10*density),(int)(10*density),(int)(10*density));
        imgBtn.setOnClickListener(l);
        if(btnType==BTN.LEFT) {
            leftGroup.addView(imgBtn);
        }else{
            rightGroup.addView(imgBtn);
        }
    }
    public void addTextBtn(BTN btnType,Object tag,String btnText,View.OnClickListener l){
        TextView imgBtn=new TextView(this.getContext());
        imgBtn.setTag(tag);
        imgBtn.setText(btnText);
        imgBtn.setTextColor(styleConfig.optInt("textColor",0xFFFFFFFF));
        imgBtn.setPadding((int)(10*density),(int)(10*density),(int)(10*density),(int)(10*density));
        imgBtn.setOnClickListener(l);
        if(btnType==BTN.LEFT) {
            leftGroup.addView(imgBtn);
        }else{
            rightGroup.addView(imgBtn);
        }
    }
    public static boolean matcheBtn(View v,Object tag){
        if(tag instanceof String){
            return tag.equals(v.getTag());
        }else if(tag instanceof Integer){
            return tag==v.getTag();
        }
       return false;
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }
}
